<?php

namespace App\Http\Controllers\Backend\Shop;

use App\ConstDir\ErrorConst;
use App\Http\Controllers\Backend\BaseController;
use Illuminate\Http\Request;
use App\Repository\Repositories\Interfaces\ShopLevelRepository;

/**
 * 后台店铺模块
 *
 * @package App\Http\Controllers\Backend\ShopType
 */
class ShopLevelController extends BaseController
{

    protected $type_repo;

    public function __construct(ShopLevelRepository $type_repo)
    {
        $this->type_repo = $type_repo;
    }

    public function index()
    {
        $list = $this->type_repo->paginate($this->perPage);

        return $this->success(ErrorConst::SUCCESS_CODE, $list,true);
    }

    public function create(Request $request)
    {
        $this->validate($request,[
            'lv_txt_tag'=> 'required',
            'lv_min_grow'=>'required|integer',
            'lv_max_grow'=>'required|integer',
        ]);

        $data = $request->all();
        $ret = $this->type_repo->create($data);
        if($ret){
            return $this->success(ErrorConst::SUCCESS_CODE, $ret['data']);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }

    }

    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'lv_txt_tag'=> 'required',
            'lv_min_grow'=>'required|integer',
            'lv_max_grow'=>'required|integer',
        ]);
        $data = $request->all();

        $ret = $this->type_repo->update($data, $id);
        if($ret){
            return $this->success(ErrorConst::SUCCESS_CODE, ErrorConst::SUCCESS_CODE_MSG);
        }else{
            return $this->error(ErrorConst::FAILED_CODE);
        }
    }

    public function delete($id)
    {
        $ret = $this->type_repo->delete($id);

        if ($ret) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }


}